package com.group.app.example.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

//@Configuration
public class MysqlConig {

    @Autowired
    private Environment environment;

    @Bean(name="mysqlDataSource")
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(environment.getProperty("mysql.url"));
        druidDataSource.setDriverClassName(environment.getProperty("mysql.driver-class-name"));
        druidDataSource.setUsername(environment.getProperty("mysql.user"));
        druidDataSource.setPassword(environment.getProperty("mysql.password"));
        druidDataSource.setMaxActive(Integer.valueOf(environment.getProperty("mysql.max-active")));
        druidDataSource.setInitialSize(Integer.valueOf(environment.getProperty("mysql.initialSize")));
        return druidDataSource;
    }

    @Bean(name = "mysqlJdbcTemplate")
    public JdbcTemplate pgJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
